package com.hzeport.bgs22emduty.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.DutyScheduleExcelFailVO;
import com.hzeport.bgs22emduty.entity.DutyScheduleInfo;
import com.hzeport.bgs22emduty.outbound.*;
import com.hzeport.bgs22emduty.service.BaseDeptInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author hzeport
 */
@Slf4j
public class ExcelUtils {



    private static final String filename = "text.xlsx";


    public static String importSchedulePerson(MultipartFile multipartFile, DutyScheduleInfo dutyScheduleExcelVO, DutyScheduleInfoRepository dutyScheduleInfoService, BaseDeptInfoRepository baseDeptInfoRepository, BasePersonInfoRepository basePersonInfoRepository, String excelFileUrl, UserInfoDetailVO userInfoDetail, BaseDeptInfoService baseDeptInfoService, ParamDutyRepository paramDutyRepository, DutyScheduleInfoRepository dutyScheduleInfoRepository, DutySignInfoRepository dutySignInfoRepository, HttpServletResponse response, DutyScheduleSetCustomRepository dutyScheduleSetCustomRepository, GuavacacheUtils guavacacheUtils) {
        InputStream inputStream=null;
        InputStream temp=null;
        try {
            //将流copy到file
            inputStream = multipartFile.getInputStream();
            File file = FileUtils.getFile(filename);
            FileUtils.copyInputStreamToFile(inputStream,file);
            temp=multipartFile.getInputStream();
            ExcelReader excelReader = EasyExcel.read(temp, DutyScheduleExcelFailVO.class, new ExcelDataVerifyHandler(dutyScheduleExcelVO,dutyScheduleInfoService,baseDeptInfoRepository,basePersonInfoRepository,excelFileUrl,userInfoDetail,baseDeptInfoService, paramDutyRepository,dutyScheduleInfoRepository,dutySignInfoRepository, dutyScheduleSetCustomRepository, response,dutyScheduleSetCustomRepository, guavacacheUtils)).build();
            ReadSheet readSheet = EasyExcel.readSheet().build();
            excelReader.read(readSheet);
            return "";
        }catch (Exception e){

            log.error("导入错误",e);
            return e.getMessage();
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException ioException) {
                    log.error("关闭流异常：",ioException);
                }
            }
            if (temp!=null){
                try {
                    temp.close();
                } catch (IOException ioException) {
                    log.error("关闭流异常：",ioException);
                }
            }
        }
//        return "";
    }

    public static String importZGSchedulePerson(MultipartFile multipartFile, DutyScheduleInfo dutyScheduleExcelVO, DutyScheduleInfoRepository dutyScheduleInfoRepository, BaseDeptInfoRepository baseDeptInfoRepository, BasePersonInfoRepository basePersonInfoRepository, String excelFileUrl, UserInfoDetailVO userInfoDetail, BaseDeptInfoService baseDeptInfoService, ParamDutyRepository paramDutyRepository, DutySignInfoRepository dutySignInfoRepository, HttpServletResponse response, DutyScheduleSetCustomRepository dutyScheduleSetCustomRepository, GuavacacheUtils guavacacheUtils) {
        InputStream inputStream=null;
        InputStream temp=null;
        try {
            //将流copy到file
            inputStream = multipartFile.getInputStream();
            File file = FileUtils.getFile(filename);
            FileUtils.copyInputStreamToFile(inputStream,file);
            temp=multipartFile.getInputStream();
            ExcelReader excelReader = EasyExcel.read(temp, DutyScheduleExcelFailVO.class, new ExcelZGDataVerifyHandler(dutyScheduleExcelVO,dutyScheduleInfoRepository,baseDeptInfoRepository,basePersonInfoRepository,excelFileUrl,userInfoDetail,baseDeptInfoService, paramDutyRepository,dutyScheduleInfoRepository,dutySignInfoRepository, dutyScheduleSetCustomRepository, response,dutyScheduleSetCustomRepository, guavacacheUtils)).build();
            ReadSheet readSheet = EasyExcel.readSheet().build();
            excelReader.read(readSheet);
            return "";
        }catch (Exception e){
            log.error("导入错误",e);
            return e.getMessage();
        }finally {
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException ioException) {
                    log.error("关闭流异常：",ioException);
                }
            }
            if (temp!=null){
                try {
                    temp.close();
                } catch (IOException ioException) {
                    log.error("关闭流异常：",ioException);
                }
            }
        }
    }
}
